var store = null; 

$(document).ready(function(){	
	// APP SETUP -> IS THERE AN EXISTING COOKIE, OR IS THE USERNAME/PASSWORD COMBO STORED?
	try{
		store = window.localStorage;
	}catch(err){
		addError( 'We cannot access the store.' );
	}
	loginProcess();
	init();
});

function init(){
	alert( 'initing' );
	document.addEventListener("deviceready", onDeviceReady, false);
	checkConnection();
}

function onDeviceReady() {
	checkConnection();
}

function addError( newErrorMsg ){
	var curErrorMsg = $('#error_message').html();
	if( curErrorMsg == 'no errors' ){
		$('#error_message').html( newErrorMsg );
	}else{
		$('#error_message').html( curErrorMsg + '<br>' + newErrorMsg );
	}		
}

function loginProcess(){
	if( store && store.getItem( "username" ) != null ){
		$( '#splash' ).hide();
		$( '#main_div' ).show();
	}else{
		$( '#splash' ).show();
		$( '#login-fieldset' ).show();
		$( '#main_div' ).hide();
	}
}

function login(){
	$('#logging-in-spinner').hide();
	$('#login-fieldset').hide();
	$('#main_div').show();
	var request = $.ajax({
		url: 'Some Server',
		type: "POST",
		data: {id : 'id'},
		dataType: "html",
		done: function( msg ){},
		fail: function(jqXHR, textStatus) {
			addError( 'could not reach login server' );
			$( '#logging-in-spinner' ).hide();
			$( '#login-fieldset' ).show();
			$('#main_div').show();
		},
		success: function( response ){
			var o = jQuery.parseJSON( response );
			if( o.success == true ){
				store.setItem( "username", o.username );
				$( '#splash' ).hide();
				$( '#main_div' ).show();
			}else{
				$('#logging-in-message').html( 'There was a problem with your credentials, please try again.' );
				$('#logging-in-message').show();
				$('#logging-in-spinner').hide();
				$('#login-fieldset').show();
			}
		}
	});
}

function logout(){
	store.setItem( "username", null );
	$( '#splash' ).show();
	$( '#login-fieldset') .show();
	$( '#logging-in-spinner' ).hide();
	$( '#main_div' ).hide();
}

function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                        'Longitude: '          + position.coords.longitude             + '<br />' +
                        'Altitude: '           + position.coords.altitude              + '<br />' +
                        'Accuracy: '           + position.coords.accuracy              + '<br />' +
                        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                        'Heading: '            + position.coords.heading               + '<br />' +
                        'Speed: '              + position.coords.speed                 + '<br />' +
                        'Timestamp: '          + 
	position.timestamp + '<br />';
}
// onError Callback receives a PositionError object
function onError(error) {
    alert('code: '    + error.code    + '\n' + 'message: ' + error.message + '\n');
}

function checkConnection() {
	alert( 'checking conn' );
	try{
		var networkState = navigator.network.connection.type;
		alert( networkState );
	    var states = {};
		states[Connection.UNKNOWN]  = 'Unknown connection';
	    states[Connection.ETHERNET] = 'Ethernet connection';
		states[Connection.WIFI]     = 'WiFi connection';
		states[Connection.CELL_2G]  = 'Cell 2G connection';
	    states[Connection.CELL_3G]  = 'Cell 3G connection';
		states[Connection.CELL_4G]  = 'Cell 4G connection';
	    states[Connection.NONE]     = 'No network connection';
	    alert('Connection type: ' + states[networkState]);
	    return true;
	}catch(err){
		alert( err );
		return false;	
	} 
}
